package com.ruoyi.waterManagement.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.domain.business.waterManagement.StManstatR;
import com.ruoyi.common.core.domain.business.waterManagement.StStationR;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.waterManagement.mapper.StManstatRMapper;
import com.ruoyi.waterManagement.service.IStManstatRService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 水情人工观测站数据记录Service业务层处理
 * 
 * @author hwd
 * @date 2023-02-21
 */
@Service
public class StManstatRServiceImpl implements IStManstatRService
{
    @Autowired
    private StManstatRMapper stManstatRMapper;

    /**
     * 根据测站代码及上报时间查询水情人工观测站数据记录
     * 
     * @param stManstatR 水情人工观测站数据记录主键
     * @return 水情人工观测站数据记录
     */
    @Override
    public StManstatR selectStManstatRByStatCdAndCheckTime(StManstatR stManstatR)
    {
        return stManstatRMapper.selectStManstatRByStatCdAndCheckTime(stManstatR);
    }

    /**
     * 查询水情人工观测站数据记录列表
     * 
     * @param stManstatR 水情人工观测站数据记录
     * @return 水情人工观测站数据记录
     */
    @Override
    public List<StManstatR> selectStManstatRList(StManstatR stManstatR)
    {
        if (stManstatR.getChkTime()!=null){
            String [] strings= DateUtils.jsYmd(stManstatR.getChkTime());
            String yy =strings[0];
            String mm =strings[1];
            String dd =strings[2];
            stManstatR.setYy(yy);
            stManstatR.setMm(mm);
            stManstatR.setDd(dd);
        }
        return stManstatRMapper.selectStManstatRList(stManstatR);
    }

    /**
     * 新增水情人工观测站数据记录
     * 
     * @param stManstatR 水情人工观测站数据记录
     * @return 结果
     */
    @Override
    public int insertStManstatR(StManstatR stManstatR)
    {
        return stManstatRMapper.insertStManstatR(stManstatR);
    }

    /**
     * 修改水情人工观测站数据记录
     * 
     * @param stManstatR 水情人工观测站数据记录
     * @return 结果
     */
    @Override
    public int updateStManstatR(StManstatR stManstatR)
    {
        return stManstatRMapper.updateStManstatR(stManstatR);
    }

    /**
     * 根据测站代码及上报时间删除水情人工观测站数据记录
     * 
     * @param stManstatR 需要删除的水情人工观测站数据记录主键
     * @return 结果
     */
    @Override
    public int deleteStManstatRByStatCdAndCheckTime(StManstatR stManstatR)
    {
        return stManstatRMapper.deleteStManstatRByStatCdAndCheckTime(stManstatR);
    }

//    /**
//     * 删除水情人工观测站数据记录信息
//     *
//     * @param statCd 水情人工观测站数据记录主键
//     * @return 结果
//     */
//    @Override
//    public int deleteStManstatRByStatCd(String statCd)
//    {
//        return stManstatRMapper.deleteStManstatRByStatCd(statCd);
//    }


    @Override
    public List<String> selMissingTime(StManstatR stManstatR) throws Exception {
        if (stManstatR!=null){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            //查询测站这天上报的所有时间
            List<StManstatR> chList=  this.selectStManstatRList(stManstatR);
            List<String> chDate=new ArrayList<>();
            if (chList!=null){
                for (StManstatR c:chList
                ) {
                    chDate.add(new SimpleDateFormat("yyyy-MM-dd HH:mm:00").format(c.getChkTime()));
                }
            }
            Date dBegin = sdf.parse(new SimpleDateFormat("yyyy-MM-dd 00:00:00").format(stManstatR.getChkTime()));
            Date dEnd = sdf.parse(new SimpleDateFormat("yyyy-MM-dd 23:59:59").format(stManstatR.getChkTime()));
            if (dEnd.after(new Date())){
                dEnd =new Date();
            }
            // int sb =Integer.parseInt(channelWaterRegimeMapper.selStationByStCode(channelWaterRegime.getStCode()).getTrnsmiFs());
            int sb =60;
            // int time =60/sb;
            List<String> list = findDates("N", dBegin, dEnd,sb);
            list.removeAll(chDate);
            return list;
        }else {
            return null;
        }
    }
    /**
     * 按照指定小时分割时间段
     * @param dateType 类型 M/D/H/N -->每月/每天/每小时/每分钟
     * @param dBegin 开始时间
     * @param dEnd 结束时间
     * @param time 指定小时(如：1、2、3、4)
     * @return
     */
    public static List<String> findDates(String dateType, Date dBegin, Date dEnd,int time) throws Exception {
        List<String> listDate = new ArrayList<>();
        listDate.add(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(dBegin));
        Calendar calBegin = Calendar.getInstance();
        calBegin.setTime(dBegin);
        Calendar calEnd = Calendar.getInstance();
        calEnd.setTime(dEnd);
        while (calEnd.after(calBegin)) {
            if ("H".equals(dateType)){
                calBegin.add(Calendar.HOUR, time);
            }
            if ("M".equals(dateType)){
                calBegin.add(Calendar.MONTH, time);
            }
            if ("D".equals(dateType)){
                calBegin.add(Calendar.DATE, time);
            }
            if ("N".equals(dateType)){
                calBegin.add(Calendar.MINUTE , time);
            }
            if (calEnd.after(calBegin)){
                listDate.add(new SimpleDateFormat("yyyy-MM-dd HH:mm:00").format(calBegin.getTime()));
            }
            else{
                // listDate.add(new SimpleDateFormat("yyyy-MM-dd HH:mm").format(calEnd.getTime()));
            }

        }
        return listDate;
    }
}
